import 'dart:math' as math;

import 'package:arith/arith.dart';
import 'package:test/test.dart';

const _expression = '-1 + (x + (-2.9 - (-3 * -4 / -5 + -~-6 - sum(7.1, 8.9))) + (sqrt(9) - 10) * -11 + y) + 13.8';

num _sum(List<num> values) => values.fold(0, (value, element) => value + element);

num _sqrt(List<num> values) => values.single * values.single;

num _max(List<num> values) => values.reduce(math.max);

void main() {
  group('A group of tests', () {
    setUp(() {
      // Additional setup goes here.
    });
    final variables = {'x': () => 1000000.1, 'y': () => 898.978};
    final parser = MathParser(_expression)
      ..callable('sum', _sum)
      ..variables = variables;

    test('eval', () {
      final e1 = -2.9 - (-3 * -4 / -5 + -~ -6 - _sum([7.1, 8.9]));
      final e2 = math.sqrt(9) - 10;
      final a = variables['x']!.call();
      final p = variables['y']!.call();
      final n = -1 + (a + e1 + e2 * -11 + p) + 13.8;
      expect(parser.evaluate(), equals(n));
    });
    test('toString', () {
      expect(parser.build().toString(), equals(_expression));
    });
  });
  group('A group of tests', () {
    setUp(() {
      // Additional setup goes here.
    });
    final parser = MathParser('sum(1,2) + -sqrt(9)')
      ..callable('sum', _sum)
      ..callable('sqrt', _sqrt);

    test('eval', () {
      expect(parser.evaluate(), equals(_sum([1, 2]) + -_sqrt([9])));
    });
  });
  group('A group of tests', () {
    setUp(() {
      // Additional setup goes here.
    });
    final parser = MathParser('max(1,2,3,4,5,6,7)')..callable('max', _max);

    test('eval', () {
      expect(parser.evaluate(), equals(_max([1, 2, 3, 4, 5, 6, 7])));
    });
  });
}
